package ch.unisi.inf.pfii.teamwhite.risk.core.io;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

//This class is used to read a file which contains the neighbours of each planet depending on the chosen map
public final class MapReader {

	public MapReader() {
	}
	
//	This method returns an ArrayList which contains Arrays of split lines of the file
	public final ArrayList<String[]> getRegions(final String fileName) {
		ArrayList<String[]> regions = new ArrayList<String[]>();
		for (int i = 0; i < 5; i++) {
			regions.add(lister(("Region" + i), (fileName + "RegionValues.txt")));
		}
		return regions;
	}

//	This method, given a line of text read from the file, returns an Array which contains the line split each space
	public final String[] lister(final String planetName, final String fileName) {
		String toSplit = fileReader(planetName, fileName);
		String[] neighboursNames = toSplit.split(" ");
		return neighboursNames;
	}

//	 This method reads the correct line from a file searching the name of the planet and returns the entire line
	private final String fileReader(final String planetName, final String fileName) {
		String result = "";
		try {
			DataInputStream in = new DataInputStream(getClass().getResourceAsStream("maps/" + fileName));
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				if (strLine.startsWith(planetName)) {
					result = strLine;
				}
			}
			in.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
		return result;
	}
}
